有時可能存在需要執行代碼塊多次的情況。循環語句允許我們執行一個語句或語句組多次,下面是循環語句的一般形式:
碰到狀況(condition)進行判斷,情況為true
,跑 Conditional Code; 情況為false
,跳過 Conditional Code。
以下有幾種類型:
只要條件為true
,重複執行目標語句(statement)。
while(condition)
{
statement(s);
}
statement(s) 可能是單個語句或語句塊,condition 可以是任何的情況。
當條件為true
時,一直執行 statement,直到條件為false
為止。
ex.
int main() {
// 一開始的年齡為 10
int age = 10;
while (age < 20) {
NSLog(@"value of age: %d",age);
age++; //age = age + 1
}
return 0;
}
結果:
2021-09-13 01:43:26.071385+0800 TestOC[98218:2703900] value of age: 10
2021-09-13 01:43:26.074459+0800 TestOC[98218:2703900] value of age: 11
2021-09-13 01:43:26.074598+0800 TestOC[98218:2703900] value of age: 12
2021-09-13 01:43:26.074709+0800 TestOC[98218:2703900] value of age: 13
2021-09-13 01:43:26.074848+0800 TestOC[98218:2703900] value of age: 14
2021-09-13 01:43:26.074956+0800 TestOC[98218:2703900] value of age: 15
2021-09-13 01:43:26.075059+0800 TestOC[98218:2703900] value of age: 16
2021-09-13 01:43:26.075151+0800 TestOC[98218:2703900] value of age: 17
2021-09-13 01:43:26.075238+0800 TestOC[98218:2703900] value of age: 18
2021-09-13 01:43:26.075315+0800 TestOC[98218:2703900] value of age: 19
跟 while 相比,較可以控制循環次數。
for ( init; condition; increment )
{
statement(s);
}
init
,這個步驟可以聲明或初始化任何循環變量,也可以不把聲明放這裡,只要一個分號出現。true
,執行 statement。若為false
,直接離開循環。true
,重複過程;若為false
,循環終止。ex.
#import <Foundation/Foundation.h>
int main() {
// 一開始的年齡為 10
int age;
for (age = 10; age < 20; age++) {
NSLog(@"value of age: %d",age);
}
return 0;
}
結果:
2021-09-13 08:26:46.041283+0800 TestOC[98565:2723233] value of age: 10
2021-09-13 08:26:46.041812+0800 TestOC[98565:2723233] value of age: 11
2021-09-13 08:26:46.041891+0800 TestOC[98565:2723233] value of age: 12
2021-09-13 08:26:46.041971+0800 TestOC[98565:2723233] value of age: 13
2021-09-13 08:26:46.042048+0800 TestOC[98565:2723233] value of age: 14
2021-09-13 08:26:46.042123+0800 TestOC[98565:2723233] value of age: 15
2021-09-13 08:26:46.042202+0800 TestOC[98565:2723233] value of age: 16
2021-09-13 08:26:46.042890+0800 TestOC[98565:2723233] value of age: 17
2021-09-13 08:26:46.043247+0800 TestOC[98565:2723233] value of age: 18
2021-09-13 08:26:46.043450+0800 TestOC[98565:2723233] value of age: 19
與前兩者不同的地方在於,do...while循環 先執行目標語句(statement),再跑條件。這使得 do ... while循環保證至少執行一次。
do
{
statement(s);
}while( condition );
條件句在循環結束後,所以 statement 至少可以執行一次。
如果條件為true
,控制流會循環執行 statement,直到條件為false
。
ex.
#import <Foundation/Foundation.h>
int main() {
// 一開始的年齡為 10
int age = 10;
do {
NSLog(@"value of age: %d",age);
age ++;
} while (age < 20);
return 0;
}
結果:
2021-09-13 10:25:54.404092+0800 TestOC[99161:2767015] value of age: 10
2021-09-13 10:25:54.404767+0800 TestOC[99161:2767015] value of age: 11
2021-09-13 10:25:54.404909+0800 TestOC[99161:2767015] value of age: 12
2021-09-13 10:25:54.405042+0800 TestOC[99161:2767015] value of age: 13
2021-09-13 10:25:54.405207+0800 TestOC[99161:2767015] value of age: 14
2021-09-13 10:25:54.405303+0800 TestOC[99161:2767015] value of age: 15
2021-09-13 10:25:54.405408+0800 TestOC[99161:2767015] value of age: 16
2021-09-13 10:25:54.405508+0800 TestOC[99161:2767015] value of age: 17
2021-09-13 10:25:54.405619+0800 TestOC[99161:2767015] value of age: 18
2021-09-13 10:25:54.405706+0800 TestOC[99161:2767015] value of age: 19
有兩種用法:
break
,循環立即終止。如果正在使用嵌套循環(循環內還有循環),break
將停止執行最深層的循環。
break;
ex.
#import <Foundation/Foundation.h>
int main() {
// 一開始的年齡為 10
int age = 10;
while (age < 20) {
NSLog(@"value of age: %d",age);
age ++;
if (age > 15) {
break;
}
}
return 0;
}
當age > 15
就會結束這循環。
結果:
2021-09-13 11:00:35.905066+0800 TestOC[99396:2786775] value of age: 10
2021-09-13 11:00:35.908529+0800 TestOC[99396:2786775] value of age: 11
2021-09-13 11:00:35.908625+0800 TestOC[99396:2786775] value of age: 12
2021-09-13 11:00:35.908711+0800 TestOC[99396:2786775] value of age: 13
2021-09-13 11:00:35.908797+0800 TestOC[99396:2786775] value of age: 14
2021-09-13 11:00:35.908871+0800 TestOC[99396:2786775] value of age: 15
continue語句的工作原理有點像 break語句,差別在於break
是強制終止整個循環,continue
是強制終止當前循環,但還在循環內。
continue;
ex.
#import <Foundation/Foundation.h>
int main() {
// 一開始的年齡為 10
int age = 10;
do {
age ++;
if (age == 15) {
NSLog(@"Hello, world");
continue;
}
NSLog(@"value of age: %d",age);
} while (age < 20);
return 0;
}
結果:
2021-09-15 17:17:21.479747+0800 TestOC[81114:955281] value of age: 11
2021-09-15 17:17:21.480617+0800 TestOC[81114:955281] value of age: 12
2021-09-15 17:17:21.480724+0800 TestOC[81114:955281] value of age: 13
2021-09-15 17:17:21.480826+0800 TestOC[81114:955281] value of age: 14
2021-09-15 17:17:21.480923+0800 TestOC[81114:955281] Hello, world
2021-09-15 17:17:21.481009+0800 TestOC[81114:955281] value of age: 16
2021-09-15 17:17:21.481085+0800 TestOC[81114:955281] value of age: 17
2021-09-15 17:17:21.481182+0800 TestOC[81114:955281] value of age: 18
2021-09-15 17:17:21.481303+0800 TestOC[81114:955281] value of age: 19
2021-09-15 17:17:21.481643+0800 TestOC[81114:955281] value of age: 20
value of age: 15
被跳過了。
只要不給條件,這個判斷舊永遠不會是 false,這個循環也就不會結束,變成了無限循環。
#import <Foundation/Foundation.h>
int main() {
for ( ; ; ) {
NSLog(@"This loop will run forever.");
}
return 0;
}